from flask_shop.main import db, Base
# https://github.com/pallets-eco/flask-sqlalchemy
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from sqlalchemy import Integer, String, ForeignKey

# Werkzeug 是一个 Python 的 WSGI 工具库，它可以用来构建 Web 应用程序
# wsgi全称为Web Server Gateway Interface，是为Python语言定义的Web服务器和Web应用程序或框架之间的一种简单而通用的接口。类似于Java语言中的Servlet
from werkzeug.security import generate_password_hash, check_password_hash


class User(db.Model):
    """
    用户模型
    """
    __tablename__ = 't_user'
    id: Mapped[int] = mapped_column(Integer, primary_key=True, init=False)
    account: Mapped[str] = mapped_column(String(32), unique=True, nullable=False)
    # 已经加密过的密码
    pwd: Mapped[str] = mapped_column(String(256), nullable=False)
    name: Mapped[str] = mapped_column(String(32), nullable=False)
    phone: Mapped[str] = mapped_column(String(11), nullable=True)
    email: Mapped[str] = mapped_column(String(32), nullable=False)

    role_id: Mapped[int] = mapped_column(Integer, ForeignKey('t_role.id'))
    role = {}


    # 加密密码
    @staticmethod
    def encrypt_password(pwd: str) -> str:
        encrypted_pwd = generate_password_hash(pwd)
        return encrypted_pwd

    @staticmethod
    def check_password(encrypted_pwd, pwd) -> bool:
        return check_password_hash(encrypted_pwd, pwd)

    # def __repr__(self):
    #     return f"User(id={self.id!r}, accounnt={self.account!r}, name={self.name!r}, phone={self.phone!r}, email={self.email!r}, role={self.role!r})"


class Role(db.Model):
    """
    角色表
    """
    __tablename__ = "t_role"
    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    role_name: Mapped[str] = mapped_column(String(32), unique=True)
    role_desc: Mapped[str] = mapped_column(String(128))

    users = relationship('User', backref='backref_role')


# class Menu(db.Model):
#     """
#     菜单
#     """
#     __tablename__ = "t_menu"
#     id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=False)
#     name: Mapped[str] = mapped_column(String(32), unique=True)
#     level: Mapped[int] = mapped_column(Integer)
#     path: Mapped[str] = mapped_column(String(32), nullable=True)
#
#     # 父菜单
#     pid: Mapped[int] = mapped_column(Integer, ForeignKey('t_menu.id'), nullable=True)
#     children: [] = relationship("Menu")
#
#     """
#     insert into t_menu (id,name,level) value (0,'全部',0);
#     insert into t_menu (id,name,level,pid) value (2,'用户管理',1,0);
#     insert into t_menu (id,name,level,pid) value (3,'权限管理',1,0);
#     insert into t_menu (id,name,level,pid) value (4,'商品管理',1,0);
#     insert into t_menu (id,name,level,pid) value (5,'订单管理',1,0);
#     insert into t_menu (id,name,level,pid) value (6,'数据统计',1,0);
#     insert into t_menu (id,name,level,pid,path) value (21,'用户列表',2,2,'/user_list');
#     insert into t_menu (id,name,level,pid,path) value (31,'角色列表',2,3,'/author_list');
#     insert into t_menu (id,name,level,pid,path) value (32,'权限列表',2,3,'/role_list');
#     insert into t_menu (id,name,level,pid,path) value (41,'商品列表',2,4,'/product_list');
#     insert into t_menu (id,name,level,pid,path) value (51,'分类列表',2,5,'/group_list');
#     """
